package models;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.*;
import play.db.jpa.Model;

@Entity
public class Event extends Model {

	public enum Category {
		Trips(null),
			MountainTrip(Trips),
			BikeTrip(Trips),
			VisitingTrip(Trips),
		NightLife(null),
			Clubs(NightLife),
			Bars(NightLife),
			Restaurants(NightLife),
		Entertainment(null),
			Cinema(Entertainment),
			Dancing(Entertainment),
			Shows(Entertainment),
		Culture(null),
			Museum(Culture),
			Theater(Culture),
			FilmAndPhoto(Culture),
		Learning(null),
			LanguageTandems(Learning),
			FreeCourses(Learning),
			Conferences(Learning),
		Sport(null),
			Football(Sport),
			BasketBall(Sport),
			Tennis(Sport),
		;
		
		public String name = this.toString().toLowerCase();
		private Category parent = null;
		private List<Category> children = new ArrayList<Category>();
		
		private Category(Category parent) {
			this.parent = parent;
			if(this.parent!=null) {
				this.parent.addChild(this);
			}
		}
		
		public Category getParent() {
			return this.parent;
		}
		
		public void addChild(Category category) {
			children.add(category);
		}
		
		public List<Category> getChildren() {
			return this.children;
		}
		
		public static List<Category> getPrimaryCategories() {
			List<Category> primaryCategories = new ArrayList<Category>();
			for(Category category: Category.values()) {
				if(category.getParent()==null) {
					primaryCategories.add(category);
				}
			}
			return primaryCategories;
		}
	}
	
	@ManyToMany(cascade=CascadeType.ALL)
	public List<User> registeredUsers;
	
	@ManyToOne
	public User author;
	
	public String title;
	public String description;
	public Category category;
	public String categoryString;
	public Date creationDate;
	public Date eventDate;
	public String location;
	Calendar cal = Calendar.getInstance();
	
	public String date;
	public boolean selected = false;
		
	@OneToMany(mappedBy="event", cascade=CascadeType.ALL)
	public List<Comment> comments;
	
	public Event(User author, String title, String description, Category category, Date eventDate, String location) {
		comments = new ArrayList<Comment>();
		this.author = author;
		this.title = title;
		this.description = description;
		this.category = category;
		this.eventDate = eventDate;
		this.creationDate = cal.getTime();
		this.categoryString = this.category.toString().toLowerCase();
		this.location = location;
		create();
	}
	
	public String toString() {
		return "Author: " + author + "; title: " + title + "; category: " + category;
	}
	
	public Event addComment(User user, String content) {
	    Comment newComment = new Comment(this, user, content).save();
	    this.comments.add(newComment);
	    this.save();
	    return this;
	}
	
	public void registerUser(User user) {
		registeredUsers.add(user);
		this.save();
	}
	
	public void unregisterUser(User user) {
		registeredUsers.remove(user);
		this.save();
	}
}
